
from feature_data.const_data import *
from feature_data.XmlData2DictData import Xml2Dict

from feature_data.Dict2Org_Graphfeature import Feature_org_extration
from feature_data.answer_extration import Answer_extration

from nd_utils.Graph_Cluster_util import *
from nd_utils.networkx_util import *


class Graph_org_stage():
    def __init__(self, feature_org_extration, answer_extration):
        self.feature_org_extration = feature_org_extration

        self.init_graph = self.feature_org_extration.init_graph
        self.cluster_coorg_pair_set_list = self.feature_org_extration.cluster_coorg_pair_set_list

        self.second_cluster_graph = self._get_cluster_graph()

        self.true_cluster_num = answer_extration.true_cluster_num
        self.true_paperidx_cluster_list = answer_extration.true_paperidx_cluster_list

        self.simulation_occur_flag = self._get_simulation_occur_flag()
        """
            in fact, this subgraph_list is cluster list
        """
        self.subgraph_list = self._get_subgraph_list()
        self.second_subgraph_nodes_list = self._get_second_subgraph_nodes_list()

    def _get_cluster_graph(self):
        pair_set_list = self.cluster_coorg_pair_set_list
        cluster_graph = add_pair_set_to_graph(pair_set_list, deepcopy_graph(self.init_graph),_weight=2 )
        return cluster_graph

    def _get_simulation_occur_flag(self):
        predict_cluster_num = get_subgraph_num(self.second_cluster_graph)
        if predict_cluster_num >= self.true_cluster_num:
            return True
        else:
            return False

    def _get_subgraph_list(self):
        # if self.simulation_occur_flag:
        if True:
            return get_subgraphs(self.second_cluster_graph)
        else:
            return get_subgraphs(self.init_graph)

    def _get_second_subgraph_nodes_list(self):
        return get_subgraph_nodes_list(self.subgraph_list)

def print_clust_total(cluster_list):
    paper_pair_set_list = cluster_list
    sum = 0

    union_set = []

    set_length_list = []
    for paper_pair_set in paper_pair_set_list:
        # print list(paper_pair_set), len(paper_pair_set)
        print sorted(list(paper_pair_set)), len(paper_pair_set)
        set_length_list.append(len(paper_pair_set))
        sum += len(paper_pair_set)

        union_set.extend(list(paper_pair_set))
    print "sum: ", sum
    # print sorted(list(union_set)), len(list(union_set))
    print sorted(list(set(union_set))) , len(set(union_set))
    print set_length_list

def print_subgraphs(first_stage_clust):
    for subgraph in first_stage_clust.subgraph_list:
        print subgraph.nodes(), len(subgraph.nodes())

from nd_utils.trans_clusteridxlist_paperidx_pairset import get_paperidx_pairset_list
from model_metric import Model_metric

if __name__ == '__main__':
    for xml_file_path in xml_filepath_list:
        xml2dict = Xml2Dict(xml_file_path)
        feature_org_extration = Feature_org_extration(xml2dict)
        answer_extration = Answer_extration(xml2dict)
        second_stage_clust = Graph_org_stage(feature_org_extration, answer_extration)

        print second_stage_clust.simulation_occur_flag
        # print first_stage_clust.cluster_list

        # print_subgraphs(second_stage_clust)

        # print_subgraphs(first_stage_clust)
        paperid_pairset_list = get_paperidx_pairset_list(second_stage_clust.true_paperidx_cluster_list)

        predict_paperid_pairset_list = get_paperidx_pairset_list(second_stage_clust.second_subgraph_nodes_list)

        model_metric = Model_metric(paperid_pairset_list, predict_paperid_pairset_list )

        print model_metric.pairwise_precision, model_metric.pairwise_recall, model_metric.pairwise_f1

"""
True
0.860613810742 0.741189427313 0.796449704142
True
0.985994397759 0.378494623656 0.547008547009
True
1.0 0.842710997442 0.914642609299
True
0.562266167825 0.881072026801 0.686460032626
False
0.770770770771 0.941320293399 0.84755090809
True
0.986301369863 0.487722269263 0.65269121813
True
1.0 0.933985330073 0.965865992415
True
0.995412844037 0.612994350282 0.758741258741
True
1.0 0.986425339367 0.993166287016
True
1.0 0.727578475336 0.842310188189
"""